package puzzle.puzzleup2008;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FiveQuestions {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		for (char c1 = 'A'; c1 <= 'E'; c1 ++) {
			for (char c2 = 'A'; c2 <= 'E'; c2 ++) {
				for (char c3 = 'A'; c3 <= 'E'; c3 ++) {
					for (char c4 = 'A'; c4 <= 'E'; c4 ++) {
						for (char c5 = 'A'; c5 <= 'E'; c5 ++) {
							Character[] ans = new Character[] {c1,c2,c3,c4,c5};
							if (q1(ans) && q2(ans) && q3(ans) && q4(ans) && q5(ans)) {
								System.out.println(Arrays.asList(ans));
							}
						}
					}
				}
			}
		}
	}

	private static boolean q1(Character[] ans) {
		
		//get max count
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (int i = 0; i < ans.length; i ++) {
			Character c = ans[i];
			Integer n = map.get(c);
			if (map.get(ans) == null) {
				n = 1;
			} else {
				n += 1;
			}
			map.put(c, n);
		}
		
		Integer max = -1;
		for (Character c: map.keySet()) {
			Integer n = map.get(c);
			if (n > max) {
				max = n;
			}
		}
		
		//get last question
		List<Character> tmp = new ArrayList<Character>();
		for (Character c: map.keySet()) {
			Integer n = map.get(c);
			if (n == max) {
				tmp.add(c);
			}
		}
		
		Integer index = -1;
		for (int i = 4; i > -1; i --) {
			if (tmp.contains(ans[i])) {
				index = i+1;
				break;
			}
		}
		
		//check answer
		map.clear();
		map.put('A', 1);
		map.put('B', 2);
		map.put('C', 3);
		map.put('D', 4);
		map.put('E', 5);
		
		return (map.get(ans[0]).intValue() == index.intValue()); 
	}
	
	private static boolean q2(Character[] ans) {

		Map<Character, Character> map = new HashMap<Character, Character>();
		map.put('A', 'C');
		map.put('B', 'E');
		map.put('C', 'B');
		map.put('D', 'D');
		map.put('E', 'A');
		
		return map.get(ans[1]).charValue() == ans[4].charValue();
	}
	
	private static boolean q3(Character[] ans) {

		Map<Character, Character> map = new HashMap<Character, Character>();
		map.put('A', 'E');
		map.put('B', 'C');
		map.put('C', 'D');
		map.put('D', 'A');
		map.put('E', 'B');
		
		return map.get(ans[2]).charValue() == ans[3].charValue();
	}
	
	private static boolean q4(Character[] ans) {
		
		int n = -1;
		for (int i = 0; i < ans.length; i ++) {
			if (ans[i].charValue() == 'B') {
				n = i+1;
				break;
			}
		}
		//check answer
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		map.put('A', 3);
		map.put('B', 5);
		map.put('C', 4);
		map.put('D', 1);
		map.put('E', 2);
		
		return (map.get(ans[3]).intValue() == n); 	
	}
	
	private static boolean q5(Character[] ans) {
		
		int n = 0;
		for (int i = 0; i < ans.length; i ++) {
			if (ans[i].charValue() == 'C') {
				n++;
			}
		}
		//check answer
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		map.put('A', 4);
		map.put('B', 3);
		map.put('C', 2);
		map.put('D', 1);
		map.put('E', 0);
		
		return (map.get(ans[4]).intValue() == n); 	
	}
}
